package cn.com.xf.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import cn.com.xf.dao.TcaKnowledgefileDAO;
import cn.com.xf.dao.TcaKnowledgefolderDAO;
import cn.com.xf.domain.TcaKnowledgefile;
import cn.com.xf.domain.TcaKnowledgefolder;
import cn.com.xf.service.WikiService;
import cn.com.xf.web.security.UserInfo;

@Service
public class WikeServiceImpl implements WikiService {
	@Autowired
	private TcaKnowledgefileDAO tcaKnowledgefileDAO;
	@Autowired
	private TcaKnowledgefolderDAO tcaKnowledgefolderDAO;

	@Override
	public List<TcaKnowledgefolder> selectFloderAndTitle(String floderId) {
		// 1.获取其子类
		List<TcaKnowledgefolder> subFolders = tcaKnowledgefolderDAO.selectSubFolders(floderId);
		// 2.若还有子类，继续获取
		for (TcaKnowledgefolder folder : subFolders) {
			// 获取其直属条目
			List<TcaKnowledgefolder> items = tcaKnowledgefolderDAO.selectItems4View(folder.getFolderid());
			folder.setItems(items);
			if (3 != folder.getLevelid()) {
				List<TcaKnowledgefolder> sub2Folders = tcaKnowledgefolderDAO.selectSubFolders(folder.getFolderid());
				// sub2Folders.addAll(items);
				folder.setSubFloders(sub2Folders);
				for (TcaKnowledgefolder folder2 : sub2Folders) {
					// 获取其直属条目
					List<TcaKnowledgefolder> items2 = tcaKnowledgefolderDAO.selectItems4View(folder2.getFolderid());
					folder2.setItems(items2);
					if (3 != folder.getLevelid()) {
						List<TcaKnowledgefolder> sub3Folders = tcaKnowledgefolderDAO.selectSubFolders(folder2
								.getFolderid());
						folder2.setSubFloders(sub3Folders);
						if (!CollectionUtils.isEmpty(sub3Folders)) {
							for (TcaKnowledgefolder folder3 : sub3Folders) {
								// 获取其直属条目
								List<TcaKnowledgefolder> items3 = tcaKnowledgefolderDAO.selectItems4View(folder3
										.getFolderid());
								for (TcaKnowledgefolder item : items3) {
									item.setLevelid(null);
									item.setItem(true);
								}
								folder3.setItems(items3);
							}
						}
					}
				}
			}
		}

		return subFolders;
	}

	/**
	 * 判断是否为叶子节点
	 * 
	 * @param folder
	 * @return
	 */
	private boolean isLast(TcaKnowledgefolder folder) {
		if ("2".equalsIgnoreCase(folder.getLevelid() + "")) {
			return true;
		}
		return false;
	}

	@Override
	public TcaKnowledgefile selectItemById(String itemId) {
		return tcaKnowledgefileDAO.selectByPrimaryKey(itemId);
	}

	@Override
	public List<TcaKnowledgefolder> selectAllFolder() {
		String floderId = "-1";
		// 1.获取其子类
		List<TcaKnowledgefolder> subFolders = tcaKnowledgefolderDAO.selectSubFolders(floderId);
		// 2.若还有子类，继续获取
		for (TcaKnowledgefolder folder : subFolders) {
			if (3 != folder.getLevelid()) {
				List<TcaKnowledgefolder> sub2Folders = tcaKnowledgefolderDAO.selectSubFolders(folder.getFolderid());
				folder.setSubFloders(sub2Folders);
				for (TcaKnowledgefolder folder2 : sub2Folders) {
					if (3 != folder.getLevelid()) {
						List<TcaKnowledgefolder> sub3Folders = tcaKnowledgefolderDAO.selectSubFolders(folder2
								.getFolderid());
						folder2.setSubFloders(sub3Folders);
					}
				}
			}
		}
		return subFolders;
	}

	@Override
	public int updateFolderTitle(String folderTitle, String folderId) {
		TcaKnowledgefolder folder = new TcaKnowledgefolder();
		folder.setFolderid(folderId);
		folder.setTitle(folderTitle);
		tcaKnowledgefolderDAO.updateByPrimaryKeySelective(folder);
		return 0;
	}

	@Override
	public TcaKnowledgefolder selectFolderById(String folderId) {
		return tcaKnowledgefolderDAO.selectByPrimaryKey(folderId);
	}

	@Override
	public int updateAddFolder(TcaKnowledgefolder folder) {
		tcaKnowledgefolderDAO.insert(folder);
		return 0;
	}

	@Override
	public int insertItem(String title, String folderId, String content) {
		UserInfo user = (UserInfo) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		TcaKnowledgefile item = new TcaKnowledgefile();
		item.setFolderid(folderId);
		item.setTitle(title);
		item.setContent(content);
		item.setValid("0");
		item.setAdduser(user.getName());
		item.setAddtime(new Date());
		tcaKnowledgefileDAO.insert(item);
		return 0;
	}

	@Override
	public List<TcaKnowledgefile> selectItems() {
		List<TcaKnowledgefile> list = tcaKnowledgefileDAO.selectItems();
		return list;
	}

	@Override
	public List<TcaKnowledgefolder> selectAllFolder4Option() {
		List<TcaKnowledgefolder> list = new ArrayList<TcaKnowledgefolder>();
		String floderId = "-1";
		// 1.获取其子类
		List<TcaKnowledgefolder> subFolders = tcaKnowledgefolderDAO.selectSubFolders(floderId);
		// 2.若还有子类，继续获取
		for (TcaKnowledgefolder folder : subFolders) {
			// 第一步加一级目录
			list.add(folder);
			List<TcaKnowledgefolder> sub2Folders = tcaKnowledgefolderDAO.selectSubFolders(folder.getFolderid());
			for (TcaKnowledgefolder folder2 : sub2Folders) {
				// 第二步加二级目录
				folder2.setTitle("&nbsp;&nbsp;&nbsp;&nbsp" + folder2.getTitle());
				list.add(folder2);

				List<TcaKnowledgefolder> sub3Folders = tcaKnowledgefolderDAO.selectSubFolders(folder2.getFolderid());
				for (TcaKnowledgefolder folder3 : sub3Folders) {
					// 第三步加三级目录
					folder3.setTitle("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp" + folder3.getTitle());
					list.add(folder3);
				}
			}
		}
		return list;
	}

	@Override
	public int selectItemsCount(String title, String folderId, String valid) {
		return tcaKnowledgefileDAO.selectItemsCount(title, folderId, valid);
	}

	@Override
	public List<TcaKnowledgefile> selectItems(int begin, int pageSize, String orderByField, String orderByMode,
			String title, String folderId, String valid) {
		return tcaKnowledgefileDAO.selectItems(begin, pageSize, orderByField, orderByMode, title, folderId, valid);
	}

	@Override
	public int deleteItem(String itemId) {
		return tcaKnowledgefileDAO.deleteByPrimaryKey(itemId);
	}

	@Override
	public int updateItem(String title, String folderId, String itemId, String conent) {
		UserInfo user = (UserInfo) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		TcaKnowledgefile itemOld = tcaKnowledgefileDAO.selectByPrimaryKey(itemId);
		TcaKnowledgefile item = new TcaKnowledgefile();
		item.setSeqid(itemOld.getSeqid());
		item.setFolderid(folderId);
		item.setTitle(title);
		item.setContent(conent);
		item.setUpdatetime(new Date());
		item.setUpdateuser(user.getName());
		tcaKnowledgefileDAO.updateByPrimaryKeySelective(item);
		return 0;
	}

	@Override
	public int updateAudit(String itemId, String auditStatus) {
		UserInfo user = (UserInfo) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		TcaKnowledgefile item = tcaKnowledgefileDAO.selectByPrimaryKey(itemId);
		item.setValid(auditStatus);
		item.setAuditor(user.getName());
		item.setAudittime(new Date());
		tcaKnowledgefileDAO.updateByPrimaryKeySelective(item);
		return 0;
	}
}
